package ims.servlet;

import ims.beans.Address;
import ims.beans.CustomerOrder;
import ims.beans.Product;
import ims.db.view.customer.order.ViewCustomerOrderDAO;
import ims.db.view.customer.order.ViewCustomerOrderDbRequest;
import ims.db.view.customer.order.ViewCustomerOrderDbResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ViewOrderServlet
 */
@WebServlet("/ViewOrderServlet")
public class ViewOrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewOrderServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			response.setContentType("text/html;charset=UTF-8");
	        PrintWriter out = response.getWriter();
	        
	        //generating output of page
			out.println("<html>");
            out.println("<head>");
            out.println("<title>Customer Order</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<a href='/IMS/prodType'>Product Type</a>&nbsp;&nbsp;");
            out.println("<a href='/IMS/product'>Product</a>&nbsp;&nbsp;");
            out.println("<a href='/IMS/searchCustOrder'>Search Customer Order</a>&nbsp;&nbsp;</br></br>");
            out.println("<h1>Customer Order </h1>");
            
            ViewCustomerOrderDbRequest dbRequest = new ViewCustomerOrderDbRequest();
            dbRequest.setOrderNum(request.getParameter("orderNum"));
            ViewCustomerOrderDAO db = new ViewCustomerOrderDAO();
            ViewCustomerOrderDbResponse dbResponse = (ViewCustomerOrderDbResponse)db.execute(dbRequest);
            CustomerOrder order = dbResponse.getOrder();
            
            out.println("Order Number: "+order.getOrderNum()+"</br>");
            out.println("Order Date: " + order.getOrderDt()+"</br>");
            out.println("Order Type: " + order.getOrderType()+"</br>");
            out.println("Customer name: "+order.getCustomer().getName()+"</br>");
            out.println("Customer email: "+order.getCustomer().getEmail()+"</br></br>");
            
            out.println("<table border='1'>");
            double grandTotal = 0;
            double subTotal=0;
            ArrayList  <Product> products = order.getProducts();
            out.println("<tr><th>Product Code</th><th>Product Name</th><th>Quantity</th><th>Cost per item</th><th>Price</th></tr>");
            for (Product prod: products)
            {
            	out.println("<tr><td>"+prod.getPid()+"</td>");
            	out.println("<td>"+prod.getProdDesc()+"</td>");
            	out.println("<td>"+prod.getQty()+"</td>");
            	out.println("<td>"+prod.getPrice()+"</td>");
            	subTotal = prod.getPrice() * prod.getQty();
            	out.println("<td>$"+subTotal+"</td>");
            	grandTotal= grandTotal+subTotal;
            }
            out.println("<tr><td colspan='4'>Total</td><td>"+grandTotal+"</tr>");
            out.println("</table>");
            //Shipping
            out.println("</br></br>SHIP TO: </br>");
            Address ship = order.getShippingAdd();
            out.println(ship.getFirstNm()+" "+ ship.getLastNm()+"</br>");
            out.println(ship.getStreet()+"</br>");
            out.println(ship.getCity()+", " + ship.getState() +" " +ship.getZip()+"</br></br></br>");
            
            //Billing
            out.println("BILL TO: </br>");
            Address bill = order.getBillingAdd();
            out.println(bill.getFirstNm()+" "+ bill.getLastNm()+"</br>");
            out.println(bill.getStreet()+"</br>");
            out.println(bill.getCity()+", " + bill.getState() +" " +bill.getZip()+"</br>");
            
            //Payment
            out.println("Payment Type: " + order.getPayment().getPaymentType()+"</br>");
            out.println("Credit Card #: " +order.getPayment().getCcNum()+"</br>");
            out.println("Expiration Date: " +order.getPayment().getExpDt()+"</br></br>");
            out.println(" </body> " );
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}
